import 'dart:math';

import 'package:flutter/cupertino.dart';

class FrameAnimationPainter extends CustomPainter {
  List<List<Color>> frameData;

  ///间距
  final double? pointSpace;


  FrameAnimationPainter(this.frameData,
      {this.pointSpace});

  @override
  void paint(Canvas canvas, Size size) {
    double minLength;
    double offsetX;
    double offsetY;
    if (size.width < size.height) {
      offsetX = 0;
      offsetY = (size.height - size.width) * 0.5;
      minLength = size.width;
    } else {
      offsetX = (size.width - size.height) * 0.5;
      offsetY = 0;
      minLength = size.height;
    }


    double padding;
    if(pointSpace != null){
      padding = pointSpace!;
    }else{
      double itemSize = minLength/frameData.length;
      padding = itemSize*0.2;
    }

    double pointSize =
        (minLength - ((frameData.length - 1) * padding)) /
            frameData.length;

    for (int j = 0; j < frameData.length; j++) {
      var dx = (pointSize + padding) * j + offsetX;
      for (int z = 0; z < frameData[j].length; z++) {
        var dy = (pointSize + padding) * z + offsetY;
        var pixel = frameData[j][z];
        canvas.drawRect(
            Rect.fromPoints(
                Offset(dx, dy), Offset(dx + pointSize, dy + pointSize)),
            Paint()
              ..style = PaintingStyle.fill
              ..color = pixel);
      }
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
